<?php

use \Mapper\MemberModel as MemberMapper;

class FeedbackController extends \Base\AbstractController {

    /**
     * @desc 列表
     */
    public function listAction() {
        $request      = $this->getRequest();
        $class        = (int)$request->get('class');
        $keyword      = trim((string)$request->get('keyword'));
        $type         = (int)$request->get('type');
        $memberMapper = MemberMapper::getInstance();
        $return       = [];
        $where        = [];
        $ids          = [];

        if(!empty($class) && !empty($keyword)) {
            if($class === 1) {
                $first = ['member_id' => $keyword];

            } elseif($class === 2) {
                $memberModels = $memberMapper->fetchAll(['nick_name LIKE "%' . urlencode($keyword) . '%"']);

                foreach($memberModels as $memberModel) {
                    if($memberModel instanceof \MemberModel) {
                        $ids[] = $memberModel->getId();
                    }
                }

                $first = ['member_id' => $ids];
            }
        }

        if(!empty($type)) {
            $second = ['type' => $type];
        }

        //组装查询语句
        if(!empty($first) && !empty($second)) {
            $where = array_merge($first, $second);
        } elseif(!empty($first) || !empty($second)) {
            !empty($first) ? $where = $first : $where = $second;
        }

        $page = new \Lib\Paginator('configFeedback');
        $page->where($where);
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $this->assign('count', $page->count());
        $models = $page->lists();

        foreach($models as $model) {
            $data              = $model->toArray();
            $member            = $memberMapper->findById($model->getMember_id());

            if($member instanceof MemberModel) {
                $data['nick_name'] = urldecode($member->getNick_name());
            }else{
                $data['nick_name'] = '';
            }

            $return[] = $data;
        }

        $this->assign('lists', $return);
    }

    /**
     * @desc 编辑
     */
    public function editAction() {
        $request = $this->getRequest();
        $id      = $request->get('id');
        $status  = $request->get('status');

        $feedbackModel                = \Mapper\ConfigFeedbackModel::getInstance();
        $memberModel                  = MemberMapper::getInstance();
        $feedback                     = $feedbackModel->findById($id)->toArray();
        $feedback['member_nick_name'] = urldecode($memberModel->findById($feedback['member_id'])->getNick_name());

        if($request->isPost()) {
            try {
                $rows = $feedbackModel->update(['status' => $status], ['id' => $id]);

                if($rows == 0) {
                    return \Response::msg('未修改');
                } elseif($rows > 0) {
                    return \Response::msg('处理成功');
                }

            } catch(Exception $exc) {
                return \Response::errmsg('处理失败');
            }

        }

        $this->assign('data', $feedback);
    }
}
